// 新建：清空当前配置
function newForm() {
    if (!confirm('确定要新建表单？未保存的数据将丢失。')) return;
    formConfig = {
        metadata: { title: "动态表单", version: "1.0.0" ,name: "", fields: []},
        formConfig: { fields: [], buttons: [], onLoad: [], onSave: [] }
    };
    selectedItem = null;
    fieldCounter = 0;
    actionCounter = 0;
   initializeDefinitionBlock();
    DropZones.fieldsContainer.innerHTML = '';
    DropZones.buttonContainer.innerHTML = '';
    DropZones.onloadContainer.innerHTML = '';
    DropZones.onsaveContainer.innerHTML = '';
    fieldConfigPanel.innerHTML = '<p>请选择画布中的字段进行配置</p>';
    actionConfigPanel.innerHTML = '<p>请选择生命周期中的动作进行配置</p>';
    updateJsonPreview();
    alert('已新建空白表单');
}

// 保存：导出 JSON 文件
function saveForm() {
    const jsonStr = JSON.stringify(formConfig, null, 2);
    const blob = new Blob([jsonStr], { type: 'application/json' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = (formConfig.metadata.title || 'form-config') + '.json';
    a.click();
    URL.revokeObjectURL(url);
}

// 打开：触发隐藏文件输入
function openForm() {
    document.getElementById('fileInput').click();
}

// 加载打开的 JSON 文件
function loadFormFile(event) {
    const file = event.target.files[0];
    if (!file) return;
    const reader = new FileReader();
    reader.onload = function (e) {
        try {
            const loaded = JSON.parse(e.target.result);
            // 简单校验
            if (!loaded.metadata || !loaded.formConfig) throw new Error('文件格式错误');
            formConfig = loaded;
            toFieldClass();
            toActionClass();
            // 重新渲染
            DropZones.fieldsContainer.innerHTML = '';
            DropZones.buttonContainer.innerHTML = '';
            DropZones.onloadContainer.innerHTML = '';
            DropZones.onsaveContainer.innerHTML = '';
            formConfig.formConfig.fields.forEach(f => FieldsContainer.renderField(f, DropZones.fieldsContainer));
            formConfig.formConfig.buttons.forEach(b => ActionContainer.renderAction(b, DropZones.buttonContainer));
            formConfig.formConfig.onLoad.forEach(a => ActionContainer.renderAction(a, DropZones.onloadContainer));
            formConfig.formConfig.onSave.forEach(a => ActionContainer.renderAction(a, DropZones.onsaveContainer));
            // 回填定义区
           initializeDefinitionBlock();

            selectedItem = null;
            fieldConfigPanel.innerHTML = '<p>请选择画布中的字段进行配置</p>';
            actionConfigPanel.innerHTML = '<p>请选择生命周期中的动作进行配置</p>';
            updateJsonPreview();
            alert('表单文件已加载');
        } catch (err) {
            alert('加载失败：' + err.message);
        }
    };
    reader.readAsText(file);
    // 清空 input，允许重复打开同一文件
    event.target.value = '';
}
function toFieldClass(){
    for(var i=0;i<formConfig.formConfig.fields.length;i++){
        var f = formConfig.formConfig.fields[i];
        f=new Field(f);
        formConfig.formConfig.fields[i]=f;
    }
}
function toActionClass(){
    for(var i=0;i<formConfig.formConfig.buttons.length;i++){
        var a = formConfig.formConfig.buttons[i];
        a=new Action(a);
        formConfig.formConfig.buttons[i]=a;
    }
    for(var i=0;i<formConfig.formConfig.onLoad.length;i++){
        var a = formConfig.formConfig.onLoad[i];
        a=new Action(a);
        formConfig.formConfig.onLoad[i]=a;
    }
    for(var i=0;i<formConfig.formConfig.onSave.length;i++){
        var a = formConfig.formConfig.onSave[i];
        a=new Action(a);
        formConfig.formConfig.onSave[i]=a;
    }
}